﻿/*-------------------------------------------------------------------------
* 命名空间: WaferSelectV3.DAL.LocalDataAccess/VisionOffsetAccess
* 类       名: VisionOffsetAccess
* 功      能:  TD
* 时      间:  2024-05-16 14:07:24
* 版      本:  V1.0
* 作      者:  Fioman
* 格      言:  Talk is cheap,show me the code ^_^
*-------------------------------------------------------------------------*/
using Microsoft.EntityFrameworkCore.Query.Internal;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WaferSelectV3.Common;
using WaferSelectV3.DAL.DataEntity;
using WaferSelectV3.Extensions;
using WaferSelectV3.Services;

namespace WaferSelectV3.DAL.LocalDataAccess
{
    public class VisionOffsetAccess
    {
        /// <summary>
        /// 获取补偿表的所有数据内容
        /// </summary>
        /// <returns></returns>
        public static DataResult<List<VisionOffsetEntity>> GetVisionOffsets()
        {
            DataResult<List<VisionOffsetEntity>> result = new DataResult<List<VisionOffsetEntity>>();
            try
            {
                using (var context = new DataContext())
                {
                    result.Data = context.VisionOffsets.ToList();
                    result.State = true;
                }
            }
            catch (Exception ex)
            {
                result.Message = ex.GetFullException();
            }
            return result;
        }


        /// <summary>
        /// 插入多条数据
        /// </summary>
        /// <param name="visionOffsetList"></param>
        /// <returns></returns>
        public static DataResult AddDataMany(List<VisionOffsetEntity> visionOffsetList)
        {
            DataResult result = new DataResult();
            try
            {
                foreach (var single in visionOffsetList)
                {
                    var insertRes = AddSingleData(single);
                    if (!insertRes.State)
                    {
                        result.Message = insertRes.Message;
                        return result;
                    }
                }
                result.State = true;
            }
            catch (Exception ex)
            {
                result.Message = ex.GetFullException();
            }
            return result;
        }

        // 插入一条数据
        public static DataResult AddSingleData(VisionOffsetEntity entity)
        {
            DataResult result = new DataResult();
            try
            {
                using (var context = new DataContext())
                {
                    context.VisionOffsets.Add(entity);
                    int affectedRows = context.SaveChanges();
                    if (affectedRows > 0)
                    {
                        result.State = true;
                    }
                    else
                    {
                        result.Message = ConstantProvider.DatabaseAddError;
                    }
                }
            }
            catch (Exception ex)
            {
                result.Message = ex.GetFullException();
            }
            return result;
        }

        // 更新一条数据
        public static DataResult UpdateDataSingle(VisionOffsetEntity entity)
        {
            DataResult result = new DataResult();
            try
            {
                using (var context = new DataContext())
                {
                    var oldEntity = context.VisionOffsets.FirstOrDefault(data => data.Id == entity.Id);
                    if (oldEntity != null)
                    {
                        context.Entry(oldEntity).CurrentValues.SetValues(entity);
                        int affectRows = context.SaveChanges();
                        // 注意这里如果=0,就是表示要更新的数据和旧数据相同也认为是操作成功的
                        if (affectRows >= 0)
                        {
                            result.State = true;
                        }
                        else
                        {
                            result.Message = ConstantProvider.DatabaseUpdateError;
                        }
                    }
                    else
                    {
                        result.Message = $"更新失败,要更新的数据不存在,更新的数据Id = {entity.Id},更新的数据表为 = {nameof(context.VisionOffsets)}";
                    }
                }
            }
            catch (Exception ex)
            {
                result.Message = ex.GetFullException();
            }
            return result;
        }
    }
}
